provider "alicloud" {
  profile = "default"
  version = "1.153.0"
}

data "alicloud_zones" "all" {
}

module "vpc" {
  source = "../../2-vpc"

  vpc_name           = "ebike"
  vpc_cidr           = "192.168.0.0/16"
  vpc_description    = "测试vpc"
  vpc_tags           = {
    created = "Terraform"
  }

  vswitch_name       = "ebike"
  availability_zones = data.alicloud_zones.all.ids
  vswitch_cidrs      = [for i, v in data.alicloud_zones.all.ids : format("192.168.%d.0/24", i + 1)]
  vswitch_tags       = {
    created = "Terraform"
  }
}

module "security" {
  source          = "../../3-security"
  name            = "default"
  vpc_id          = module.vpc.vpc_id
  key_pair_name   = "tf-default"
  public_key_file = file("${path.module}/public.pem")
}

module "rds-redis" {
  source = "../../4-rds-redis"

  exist_azs         = module.vpc.availability_zones
  exist_vswitch_ids = module.vpc.vswitch_ids

  instance_name     = "ebike"
  instance_type     = "rds.mysql.s2.large"
  instance_storage  = 50

  allocate_public_connection = true
  databases = [
    {
      name          = "pipeline"
      character_set = "utf8"
      description   = "pipeline"
    }
  ]

  accounts = [
    {
      name          = "devops"
      password      = "1234abc"
      type          = "Normal"
      privilege     = "ReadWrite"
      databases     = ["pipeline"]
    }
  ]

}

module "ecs_cluster" {
  source = "../"

  number_of_instances         = 1

  name                        = "ebike"
  host_name                   = "ebike"
  exist_azs                   = module.vpc.availability_zones
  exist_vswitch_ids           = module.vpc.vswitch_ids
  instance_type_family        = "ecs.c5"
  cpu_core_count              = 2
  memory_size                 = 4
  security_groups             = module.security.security_group_ids
  user_data                   = file("${path.module}/user-data.yaml")
  key_name                    = module.security.key_name
  use_num_suffix              = true
  system_disk_category        = "cloud_efficiency"
  internet_max_bandwidth_out  = 1
  associate_public_ip_address = true
  private_key                 = file("${path.module}/id_dsa")
  content                     = file("${path.module}/pipeline.sql")

  rds_connection_string       = module.rds-redis.connection_string
  rds_username                = "user1"
  rds_passpwrd                = "1234abc"
  setup_db                    = true
  triggers                    = module.rds-redis.alicloud_db_account_privilege
}